package com.mali.malibackendproduct.aop;

import com.mali.backendcommon.common.Result;
import com.mali.backendcommon.util.UserContext;
import com.mali.malibackendmodel.dto.OperationLogDTO;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author:吴键霏
 * @Date：2024/6/18 15:04
 * @Version 1.0
 */

@Component
@Aspect
@Slf4j
public class OperationLog {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Around("execution(* com.mali.backendcommon.rpc.ProductService.verifyRequest(..)) || " +
            "execution(* com.mali.backendcommon.rpc.ProductService.verifyPass(..)) || " +
            "execution(* com.mali.backendcommon.rpc.ProductService.verifyUnpass(..)) || "+
            "execution(* com.mali.backendcommon.rpc.ProductService.line(..))")
    public Result logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 操作记录
        OperationLogDTO operationLogDTO=null;
        // 获取用户信息
        Long userId= UserContext.getUser().getId();

        // 获取方法名称
        String methodName = joinPoint.getSignature().getName();

        // 获取传入的参数
        Long productId=null;
        Integer status=null;
        Object[] args = joinPoint.getArgs();
        if (args.length==2){
            productId= (Long) args[0];
            status = (Integer) args[1];
        }else {
            // 获取商品id
            productId= (Long) args[0];
        }

        // 执行目标方法
        Result result;
        try {
            result = (Result)joinPoint.proceed();
        } catch (Throwable throwable) {
            throw throwable;
        }

        if (args.length==1 && result.getCode()==0 ){
            if (methodName.equals("verifyRequest")){
                String operator="审核中";
                String remark="商品申请审核";
                LocalDateTime createTime=LocalDateTime.now();
               operationLogDTO=new OperationLogDTO(userId,productId,operator,remark,createTime);
                log.info("发请审核:{}",operationLogDTO);
            }else if (methodName.equals("verifyPass")){
                String operator="审核通过";
                String remark="商品申请审核通过";
                LocalDateTime createTime=LocalDateTime.now();
                operationLogDTO=new OperationLogDTO(userId,productId,operator,remark,createTime);
                log.info("审核通过:{}",operationLogDTO);
            }else if (methodName.equals("verifyUnpass")){
                String operator="审核失败";
                String remark="商品申请审核不通过";
                LocalDateTime createTime=LocalDateTime.now();
                operationLogDTO=new OperationLogDTO(userId,productId,operator,remark,createTime);
                log.info("审核不通过:{}",operationLogDTO);
            }
        }else if (args.length==2 && result.getCode()==0){
            if (methodName.equals("line") && status==4){
                String operator="上线";
                String remark="商品成功上线";
                LocalDateTime createTime=LocalDateTime.now();
                operationLogDTO=new OperationLogDTO(userId,productId,operator,remark,createTime);
                log.info("商品上线:{}",operationLogDTO);
            }else if (methodName.equals("line") && status==5){
                String operator="下线";
                String remark="商品成功下线";
                LocalDateTime createTime=LocalDateTime.now();
                operationLogDTO=new OperationLogDTO(userId,productId,operator,remark,createTime);
                log.info("商品下线:{}",operationLogDTO);
            }
        }
        // 发送给rocketmq
        rocketMQTemplate.asyncSend("productLog", operationLogDTO, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("操作日志发送成功");
            }
            @Override
            public void onException(Throwable throwable) {
                log.error("日志发送失败");
            }
        });
        return result;
    }
}
